package com.hbyj.struts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;


import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.FzyaFzya03;
import com.hbyj.hbm.vo.FzyaFzya03temp;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01Id;
import com.hbyj.hbm.vo.Jbqk01temp;
import com.hbyj.hbm.vo.Members;
import com.hbyj.hbm.vo.MrBaseInf;
import com.hbyj.hbm.vo.YjczYjcz09;
import com.hbyj.hbm.vo.YjczYjcz10;
import com.hbyj.hbm.vo.YjczYjcz12;
import com.hbyj.hbm.vo.YjczYpxx01;
import com.hbyj.hbm.vo.YjsjYjsj01;
import com.hbyj.util.Constants;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

public class YpxxAction extends ActionSupport{
	private String jsonResult;
	private int total;
	private List rows = new ArrayList();
	
	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public void insertYpnrForYpxx() throws ParseException, UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String ypxx01Str = request.getParameter("ypxx01");
		JSONObject obj = JSONObject.fromObject(ypxx01Str);
		YjczYpxx01 ypxx01 = (YjczYpxx01)JSONObject.toBean(obj, YjczYpxx01.class);
		String num = ypxx01.getYpxx01000();
		String zhdBiaoshi = ypxx01.getJbqk0102();
		//拿到单点上报中得数据
		String hql = "from YjczYjcz12 where yjcz12000 = '"+num+"' and jbqk0102 ='"+zhdBiaoshi+"'"; 
		YjczYjcz12 yjcz12 = (YjczYjcz12) DbTools.findByHql(hql).get(0);
		//删除单点上报表中的内容
		String sql = "delete from YjczYjcz12 where yjcz12000 = '"+num+"' and jbqk0102 ='"+zhdBiaoshi+"'"; 
		Date sbTime = new Date();
		sbTime = new Date(sbTime.getYear(),sbTime.getMonth(),sbTime.getDate(),sbTime.getHours(),sbTime.getMinutes(),sbTime.getSeconds());
		ypxx01.setYpxx01010(sbTime);
		Members mrbase = (Members)request.getSession().getAttribute("SysUser");
		String ypxx01020 = mrbase.getEmpName();
		ypxx01.setYpxx01020(ypxx01020);
		//通过名字去查询这个人的单位
		String ypxx01030 = findDWByName(ypxx01020);
		ypxx01.setYpxx01030(ypxx01030);
		ypxx01.setYpxx01050("2");
		//////////////////////////////////////
		//此时，需要将事件找出来
		Date yjsj01022Time = yjcz12.getYjcz12010();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String yjsj01010 = sdf.format(yjsj01022Time);
		String hql1 = "from YjsjYjsj01 where yjsj01020 ='"+num+"' and yjsj01010 = '"+yjsj01010+"' and yjsjjbqk0102='"+zhdBiaoshi+"'";
		if(DbTools.findByHql(hql1).size()>0){
			YjsjYjsj01 yjsj01 = (YjsjYjsj01) DbTools.findByHql(hql1).get(0);
			//开始更改
			yjsj01.setYjsj01040(sbTime);
			yjsj01.setYjsj01050("01");
			/////事件更改结束/////////////
			if(DbTools.saveOrUpdateObj(ypxx01)&&DbTools.saveOrUpdateObj(yjsj01)){
				ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
			}
			DbTools.delete(sql);
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
		
	}
	
	public String findDWByName(String name){
		String dw ="";
		String hql = "from YjczYjcz09 where yjcz09030 = '"+name +"'";
		List list = (List)DbTools.findByHql(hql);
		if(list.size()>0){
			YjczYjcz09 yjcz09 = (YjczYjcz09)list.get(0);
			dw = yjcz09.getYjcz09020();
		}
		return dw;
	}
	
	
	
	public String queryYpxxForYpsh(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String hql = "from YjczYpxx01 where ypxx01050 = '"+2+"' order by ypxx01010";
		String countHql = "select count(*) from YJCZ_YPXX01 where YPXX01050 = '"+2+"'";
		int t = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	public void insertShnrForYpxx() throws UnsupportedEncodingException, IOException, ParseException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String addYjdcsjFlag = request.getParameter("addYjdcsjFlag");
		String addYjjcsjFlag = request.getParameter("addYjjcsjFlag");
		String addYjhssjFlag = request.getParameter("addYjhssjFlag");
		String ypxx01010Time = request.getParameter("ypxx01010Time");
		String ypxx01Str = request.getParameter("ypxx01");
		JSONObject obj = JSONObject.fromObject(ypxx01Str);
		YjczYpxx01 ypxx01 = (YjczYpxx01)JSONObject.toBean(obj, YjczYpxx01.class);
		Date shTime = new Date();
		shTime = new Date(shTime.getYear(),shTime.getMonth(),shTime.getDate(),shTime.getHours(),shTime.getMinutes(),shTime.getSeconds());
		ypxx01.setYpxx01060(shTime);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df.parse(ypxx01010Time);
		ypxx01.setYpxx01010(date);
		Members mrbase = (Members)request.getSession().getAttribute("SysUser");
		String ypxx01070 = mrbase.getEmpName();
		ypxx01.setYpxx01070(ypxx01070);
		//通过名字去查询这个人的单位
		String ypxx01080 = findDWByName(ypxx01070);
		ypxx01.setYpxx01080(ypxx01080);
		///////////////////////////////////////////
		//此时，需要将事件找出来
		String yjsj01020  =ypxx01.getYpxx01000();
		String yjsjjbqk0102 = ypxx01.getJbqk0102();
		String hql1 = "from YjsjYjsj01 where yjsj01020 ='"+yjsj01020+"'and yjsjjbqk0102='"+yjsjjbqk0102+"' and yjsj01040 = '"+ypxx01010Time+"'";
		if(DbTools.findByHql(hql1).size()>0){
			YjsjYjsj01 yjsj01 = (YjsjYjsj01) DbTools.findByHql(hql1).get(0);
			//开始改变
			yjsj01.setYjsj01030(shTime);
			yjsj01.setYjsj01050("02");
			boolean flagYjdc = true;
			boolean flagYjjc = true;
			boolean flagYjhs = true;
			//如果添加应急调查事件的标志位是true，那么添加事件
			if("true".equals(addYjdcsjFlag)){
				YjsjYjsj01 yjsjYjdc = new YjsjYjsj01();
				yjsjYjdc.setYjsj01010(shTime);
				yjsjYjdc.setYjsj01040(shTime);
				yjsjYjdc.setYjsj01085(shTime);
				yjsjYjdc.setYjsj01080(ypxx01070);
				yjsjYjdc.setYjsj01020(yjsj01020);
				yjsjYjdc.setYjsj01050("01");
				String yjdcsjNum = queryBiggestSJNum(yjsj01020.substring(0,6)+"09");
				yjsjYjdc.setYjsj01000(yjdcsjNum);
				yjsjYjdc.setYjsjjbqk0102(yjsjjbqk0102);
				yjsjYjdc.setYjsj01078("同意");
				if(DbTools.saveObj(yjsjYjdc)){
					flagYjdc = true;
				}else{
					flagYjdc = false;
					return;
				}
			}
			//
			if("true".equals(addYjjcsjFlag)){
				YjsjYjsj01 yjsjYjdc = new YjsjYjsj01();
				yjsjYjdc.setYjsj01010(shTime);
				yjsjYjdc.setYjsj01040(shTime);
				yjsjYjdc.setYjsj01085(shTime);
				yjsjYjdc.setYjsj01080(ypxx01070);
				yjsjYjdc.setYjsj01020(yjsj01020);
				yjsjYjdc.setYjsj01050("01");
				String yjdcsjNum = queryBiggestSJNum(yjsj01020.substring(0,6)+"11");
				yjsjYjdc.setYjsj01000(yjdcsjNum);
				yjsjYjdc.setYjsjjbqk0102(yjsjjbqk0102);
				yjsjYjdc.setYjsj01078("同意");
				if(DbTools.saveObj(yjsjYjdc)){
					flagYjjc = true;
				}else{
					flagYjjc = false;
					return;
				}
			}
			//
			if("true".equals(addYjhssjFlag)){
				YjsjYjsj01 yjsjYjdc = new YjsjYjsj01();
				yjsjYjdc.setYjsj01010(shTime);
				yjsjYjdc.setYjsj01040(shTime);
				yjsjYjdc.setYjsj01085(shTime);
				yjsjYjdc.setYjsj01080(ypxx01070);
				yjsjYjdc.setYjsj01020(yjsj01020);
				yjsjYjdc.setYjsj01050("01");
				String yjdcsjNum = queryBiggestSJNum(yjsj01020.substring(0,6)+"13");
				yjsjYjdc.setYjsj01000(yjdcsjNum);
				yjsjYjdc.setYjsjjbqk0102(yjsjjbqk0102);
				yjsjYjdc.setYjsj01078("同意");
				if(DbTools.saveObj(yjsjYjdc)){
					flagYjhs = true;
				}else{
					flagYjhs = false;
					return;
				}
			}
			if(DbTools.updateObj(ypxx01)&&DbTools.saveOrUpdateObj(yjsj01)&&flagYjdc&&flagYjjc&&flagYjhs){
				ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
			}else{
				ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
			}
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
		
	}
	
	
	public String queryBiggestSJNum(String yjsj01000){
		String newCode = "";
		String hql = "from YjsjYjsj01 where yjsj01000 like '"+yjsj01000+"____' order by yjsj01000 desc";
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			YjsjYjsj01 yjsj01 = (YjsjYjsj01)list.get(0);
			String code = yjsj01.getYjsj01000();
			long codeNum = Long.parseLong(code);
			newCode = String.valueOf((codeNum+1));
		}else{
			newCode = yjsj01000+"0001";
		}
		return newCode;
	}
	
	
	
	public String queryYpxxForYpgl(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String zhCode = request.getParameter("zhCode");
		String sbTimeStart = request.getParameter("sbTimeStart");
		String sbTimeEnd = request.getParameter("sbTimeEnd");
		String shTimeStart = request.getParameter("shTimeStart");
		String shTimeEnd = request.getParameter("shTimeEnd");
		String conStr = "";
		if(!"".equals(sbTimeStart)){
			conStr += "and ypxx01010 >='"+sbTimeStart+"'";
		}
		if(!"".equals(sbTimeEnd)){
			conStr += "and ypxx01010 <='"+sbTimeEnd+"'";
		}
		if(!"".equals(shTimeStart)){
			conStr += "and ypxx01060 >='"+shTimeStart+"'";
		}
		if(!"".equals(shTimeEnd)){
			conStr += "and ypxx01060 <='"+shTimeStart+"'";
		}
		String hql = "from YjczYpxx01 where ypxx01000 like '"+zhCode+"'"+conStr +"order by ypxx01010 DESC";
		String countHql = "select count(*) from YJCZ_YPXX01 where YPXX01000 like'"+zhCode+"'"+conStr;
		int t = DbTools.getDataSQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		rows = (List)DbTools.findPageHQL(first, max,hql);
		total = t;
		return SUCCESS;
	}
	
	public void deleteYPXXById() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String sql = "delete YjczYpxx01 where id ="+id;
		if(DbTools.delete(sql)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	public void updateYpxxForYpgl() throws UnsupportedEncodingException, IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String ypxx01Str = request.getParameter("ypxx01");
		JSONObject obj = JSONObject.fromObject(ypxx01Str);
		YjczYpxx01 ypxx01 = (YjczYpxx01)JSONObject.toBean(obj, YjczYpxx01.class);
		if(DbTools.updateObj(ypxx01)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	
	//点击研判信息管理的“确定”按钮之后，所调用的函数
	//动作:1.通过拿到研判信息中的编号，即ypxx01000这个值去查询jbqk01temp这张表，yjcz12这张表的id，ypxx01这张表的id。
	//			2.将拿到的结果值，插入到jbqk01这张表之中去，并生成新的编号，此编号为确定的编号
	//			3.将拿到的
	public void insertXzdToJbqk() throws UnsupportedEncodingException, IOException, ParseException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String ypxx01Str = request.getParameter("ypxx01");
		String ypxx01010Time = request.getParameter("ypxx01010Time");
		JSONObject obj = JSONObject.fromObject(ypxx01Str);
		YjczYpxx01 ypxx01 = (YjczYpxx01)JSONObject.toBean(obj, YjczYpxx01.class);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df.parse(ypxx01010Time);
		ypxx01.setYpxx01010(date);
		String code = ypxx01.getYpxx01000();
		Jbqk01temp jbqk01temp =(Jbqk01temp)findJbqk01tempByJbqk0100(code).get(0);
		String oldNum = jbqk01temp.getJbqk0100();
		String num = oldNum.substring(0,oldNum.length()-4);
		String newCode = queryTheBiggerInJBQK01(num);
		//插入jbqk01这张表
		Jbqk01Id jbqk01id = new Jbqk01Id(newCode, request.getParameter("jbqk0102"));
		Jbqk01 jbqk01 = new Jbqk01();
		jbqk01.setId(jbqk01id);
		jbqk01.setJbqk0110(request.getParameter("jbqk0110"));
		if(!"".equals((request.getParameter("jbqk0120")))){
			jbqk01.setJbqk0120(Double.valueOf(request.getParameter("jbqk0120")));
		}else{
			jbqk01.setJbqk0120(jbqk01temp.getJbqk0120());
		}
		if(!"".equals((request.getParameter("jbqk0130")))){
			jbqk01.setJbqk0130(Double.valueOf(request.getParameter("jbqk0130")));
		}else{
			jbqk01.setJbqk0130(jbqk01temp.getJbqk0130());
		}
		jbqk01.setJbqk0150(request.getParameter("jbqk0140"));
		jbqk01.setJbqk0150(request.getParameter("jbqk0150"));
		jbqk01.setJbqk0160(request.getParameter("jbqk0160"));
		jbqk01.setJbqk0170(request.getParameter("jbqk0170"));
		jbqk01.setJbqk0180(request.getParameter("jbqk0180"));
		jbqk01.setJbqk0190(request.getParameter("jbqk0190"));
		////防灾预案 记录更新 
		List listFzya = DbTools.findByProperty("FzyaFzya03temp", "fzya03000", code);
		FzyaFzya03temp fzya03Temp = null;
		FzyaFzya03 fzya03 = null;
		if(listFzya.size()>0){
			fzya03Temp = (FzyaFzya03temp)listFzya.get(0);
			fzya03 = copyTemp(fzya03Temp,newCode);
			if(DbTools.deleteObj(fzya03Temp)&&DbTools.saveObj(fzya03)){
				
			}else{
				ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
			}
		}
		/////////
		if(DbTools.saveObj(jbqk01)){//插入成功之后，依次更改yjcz12和ypxx01这两张表
			DbTools.deleteObj(jbqk01temp);
			/////////////////////////////////////////////
			//找出yjsj01
			String yjsj01020  =ypxx01.getYpxx01000();
			String biaoshi = ypxx01.getJbqk0102();
			String hql1 = "from YjsjYjsj01 where yjsj01020 ='"+yjsj01020+"' and yjsjjbqk0102 = '"+biaoshi+"' and yjsj01040 = '"+ypxx01010Time+"'";
			if(DbTools.findByHql(hql1).size()>0){
				YjsjYjsj01 yjsj01 = (YjsjYjsj01) DbTools.findByHql(hql1).get(0);
				//此时，需要去改变yjsj01中的值
				yjsj01.setYjsj01020(newCode);
				yjsj01.setYjsjjbqk0102(jbqk01.getId().getJbqk0102());
				///结束/////////
				ypxx01.setYpxx01000(newCode);
				ypxx01.setJbqk0102(jbqk01.getId().getJbqk0102());
				ypxx01.setYpxx01110("0");
				if(DbTools.updateObj(ypxx01)&&DbTools.updateObj(yjsj01)){
					ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
				}else{
					ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
				}
			}else{
				ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
			}
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	public List findYjcz12IdByYjcz12000(String code){
		String hql = "from YjczYjcz12 where yjcz12000 = '"+code+"' AND yjcz12090 = '1'";
		List list = (List)DbTools.findByHql(hql);
		return list;
	}
	
	public List findJbqk01tempByJbqk0100(String code){
		String hql = "from Jbqk01temp where jbqk0100 = '"+code+"'";
		List list = (List)DbTools.findByHql(hql);
		return list;
	}
	public FzyaFzya03 copyTemp(FzyaFzya03temp temp,String newCode){
		FzyaFzya03 fzya03 = new FzyaFzya03();
		fzya03.setFzya03000(newCode);
		fzya03.setFzya03160(temp.getFzya03160());
		fzya03.setFzya03170(temp.getFzya03170());
		fzya03.setFzya03180(temp.getFzya03180());
		fzya03.setFzya03190(temp.getFzya03190());
		fzya03.setFzya03200(temp.getFzya03200());
		fzya03.setFzya03210(temp.getFzya03210());
		fzya03.setFzya03220(temp.getFzya03220());
		fzya03.setFzya03230(temp.getFzya03230());
		fzya03.setFzya03240(temp.getFzya03240());
		fzya03.setFzya03250(temp.getFzya03250());
		fzya03.setFzya03260(temp.getFzya03260());
		fzya03.setFzya03270(temp.getFzya03270());
		fzya03.setFzya03280(temp.getFzya03280());
		fzya03.setFzya03290(temp.getFzya03290());
		fzya03.setFzya03300(temp.getFzya03300());
		fzya03.setFzya03310(temp.getFzya03310());
		fzya03.setFzya03320(temp.getFzya03320());
		fzya03.setFzya03330(temp.getFzya03330());
		fzya03.setFzya03340(temp.getFzya03340());
		fzya03.setFzya03350(temp.getFzya03350());
		fzya03.setFzya03360(temp.getFzya03360());
		fzya03.setFzya03370(temp.getFzya03370());
		fzya03.setFzya03380(temp.getFzya03380());
		fzya03.setFzya03390(temp.getFzya03390());
		fzya03.setFzya03400(temp.getFzya03400());
		fzya03.setFzya03410(temp.getFzya03410());
		fzya03.setFzya03420(temp.getFzya03420());
		fzya03.setFzya03430(temp.getFzya03430());
		fzya03.setFzya03440(temp.getFzya03440());
		fzya03.setFzya03450(temp.getFzya03450());
		fzya03.setFzya03460(temp.getFzya03460());
		fzya03.setFzya03470(temp.getFzya03470());
		fzya03.setFzya03480(temp.getFzya03480());
		fzya03.setFzya03490(temp.getFzya03490());
		fzya03.setFzya03500(temp.getFzya03500());
		return fzya03;
		
	}
	
	//查询出JBQK01表中最大的编号
	public String queryTheBiggerInJBQK01(String numCode){
		String newCode="";
		String code = numCode+"____";
		String hql = "from Jbqk01 where jbqk0100 like '"+code+"' order by jbqk0100 DESC";
		List list = (List)DbTools.findByHql(hql);
		if(list.size()>0){
			Jbqk01 jbqk = (Jbqk01)list.get(0);
			String num = jbqk.getId().getJbqk0100();
			long newCodeNum = Long.valueOf(num)+1;
			newCode = String.valueOf(newCodeNum);
		}else{
			newCode = numCode +"0001";
		}
		return newCode;
	}
	
}